//
//  SSBrowerCell.swift
//  weibo
//
//  Created by 邵帅 on 16/7/7.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit
import SDWebImage

class SSBrowerCell: UICollectionViewCell {
    
    var closure:(()->())?
    
    var photoModel: SSPhotoViewModel?{
        
        didSet{
            
            guard let imgString = photoModel?.model?.thumbnail_pic else {
                return
            }
            
            self.holderImg.image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(imgString)
            holderImg.hidden = false
            
            // 下载大图
            imageView.sd_setImageWithURL(replacePicUrl(imgString), placeholderImage: nil, options: [], progress: { (current, total) in
                
                self.holderImg.progress = CGFloat(current) / CGFloat(total)
                
                }) { (bigImg, error, _, _) in
                    
                    // 根据下载完的大图大小设置滚动范围
                    self.showBigImage(bigImg)
                    self.holderImg.hidden = true
                    
            }
            
            
        }
        
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        contentView.backgroundColor = RandomColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI() {
        
        addSubview(scrollView)
        scrollView.addSubview(holderImg)
        scrollView.addSubview(imageView)
        
        
        scrollView.backgroundColor = UIColor.clearColor()
        
        scrollView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(contentView.snp_edges)
        }
        
        holderImg.snp_makeConstraints { (make) in
            
            make.center.equalTo(scrollView.snp_center)
        }
        
    }
    
    // 滚动范围
    func showBigImage(img: UIImage) {
        
        let width = kUIScreenWidth
        let height = img.size.height/img.size.width * width
        
        let imgSize = CGSizeMake(width, height)
        
        
        scrollView.contentSize = imgSize
        
        if height < KUIScreenHeight {
            
            imageView.frame = CGRectMake(0, (KUIScreenHeight * 0.5 - imgSize.height * 0.5), width, height)
        }else {
            
            imageView.frame.size = imgSize
        }
    }
    
    // 替换图片的url为大图
    func replacePicUrl(urlString: String)-> NSURL? {
        
        let newUrlString = (urlString as NSString).stringByReplacingOccurrencesOfString("/thumbnail/", withString: "/bmiddle/")
        
        return NSURL(string: newUrlString)
    }
    
    // 保存图片的方法
    func savePicture() {
        
        guard let img = imageView.image else {
            
            return
        }
        
        UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil)
    }
    
    
    
    // 懒加载
    private lazy var scrollView: UIScrollView = {
    
        let scrollView = UIScrollView(frame: self.contentView.bounds)
        
        return scrollView
    
    }()
    
    private lazy var holderImg: SSProgressView = {
    
        let imgView = SSProgressView(frame: CGRectZero)
        
        imgView.sizeToFit()
    
        return imgView
    }()
    
    private lazy var imageView: UIImageView = {
        
        let imgView = UIImageView(frame: CGRectZero)
        
        imgView.userInteractionEnabled = true
        let gesture = UITapGestureRecognizer(target: self, action: #selector(SSBrowerCell.imgClick))
        imgView.addGestureRecognizer(gesture)
        
        return imgView
    }()
    
    
    func imgClick() {
        
        closure?()
        
    }
}
